package com.bilibili.juc.atomics;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

//class MyNumber
//{
//    AtomicInteger atomicInteger = new AtomicInteger();
//
//    public void addPlusPlus()
//    {
//        atomicInteger.getAndIncrement();
//    }
//}

class MyCounter {
    AtomicInteger atomicInteger = new AtomicInteger();

    public void increseCounter(){
        atomicInteger.getAndIncrement();
    }
}
public class CounterDemo01 {

    public static final int ThreadNUM = 50;

    public static void main(String[] args) throws InterruptedException {
        MyCounter counter = new MyCounter();
        CountDownLatch countDownLatch = new CountDownLatch(ThreadNUM);
        for(int i=1;i<=ThreadNUM;i++){
            new Thread(() -> {
                try {
                    for (int j = 1; j <=1000; j++) {
                        counter.increseCounter();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        //try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println(Thread.currentThread().getName()+"\t"+"result: "+counter.atomicInteger.get());
    }

}
